package com.iaiai.cobra.system.controller;

import ch.qos.logback.core.util.SystemInfo;
import com.iaiai.cobra.common.vo.ResultVo;
import com.iaiai.cobra.core.BaseController;
import com.iaiai.cobra.core.util.IpUtil;
import com.iaiai.cobra.platform.other.ip.PconlineIpAddress;
import com.iaiai.cobra.platform.other.ip.bean.PconlineIpAddressResponse;
import com.sun.management.OperatingSystemMXBean;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.hc.core5.http.ParseException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.net.URISyntaxException;
import java.text.DecimalFormat;
import java.util.*;

/**
 * Created with ServerController.
 * Package: com.iaiai.cobra.system.controller
 * Author: iaiai
 * Create Time: 2024/7/30 21:55
 * QQ: 176291935
 * Email: 176291935@qq.com
 * Description:
 */
@Slf4j
@RestController
@RequestMapping("/system/server")
public class ServerController extends BaseController {

    @PostMapping(value = "/index.json")
    public ResultVo index(HttpServletRequest request) throws IOException {
        Map<String,Object> map = new HashMap<>();
        SystemInfo si = new SystemInfo();
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();

        map.put("osName",System.getProperty("os.name"));    // 操作系统
        map.put("osArch",System.getProperty("os.arch"));    // 操作系统的构架
        map.put("osVersion",System.getProperty("os.version"));    // 操作系统的版本
        map.put("processors",Runtime.getRuntime().availableProcessors()+"");   //几核

        // 获得线程总数
        ThreadGroup parentThread;
        for (parentThread = Thread.currentThread().getThreadGroup(); parentThread
                .getParent() != null; parentThread = parentThread.getParent()) {}
        int totalThread = parentThread.activeCount();
        map.put("totalThread",totalThread+"");   //获得线程总数

        // 磁盘使用情况
        File[] files = File.listRoots();
        List<String> disks = new ArrayList<>();
        for (File file : files) {
            String total = new DecimalFormat("#.#").format(file.getTotalSpace() / 1024 / 1024 / 1024) + "G";
//            String free = new DecimalFormat("#.#").format(file.getFreeSpace() / 1024 / 1024 / 1024) + "G";
            String un = new DecimalFormat("#.#").format(file.getUsableSpace() / 1024 / 1024 / 1024) + "G";
            String path = file.getPath();
            disks.add(path + " => 总:" + total + ",可用空间:" + un);
//            log.debug("........free:{}",free);
        }
        map.put("disks",disks);   //磁盘使用情况

        map.put("pid",System.getProperty("PID"));   //当前的pid

        String ipv4 = IpUtil.getIpv4(request);
        map.put("ip", ipv4);

        try {
            PconlineIpAddressResponse ipAddressResponse = PconlineIpAddress.queryIpv4Address(ipv4);
            map.put("ipAddress",ipAddressResponse.getAddr());
        }catch (Exception e){
            e.printStackTrace();
        }
        map.put("datetime", DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss.SSS"));

        map.put("JAVA_HOME",System.getProperty("java.home"));   //JAVA_HOME
        map.put("JAVA_VERSION",System.getProperty("java.version"));   //JAVA_VERSION
        map.put("USER_HOME",System.getProperty("user.home"));   //USER_HOME
        map.put("USER_NAME",System.getProperty("user.name"));   //USER_NAME

        String totalMemorySize = new DecimalFormat("#.##").format(osmxb.getTotalPhysicalMemorySize() / 1024.0 / 1024 / 1024) + "G";
        map.put("totalMemorySize",totalMemorySize);   //总内存

        // 椎内存使用情况
        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
        map.put("initTotalMemorySize",new DecimalFormat("#.#").format(memoryUsage.getInit() * 1.0 / 1024 / 1024) + "M");   //总内存
        map.put("maxMemorySize",new DecimalFormat("#.#").format(memoryUsage.getMax() * 1.0 / 1024 / 1024) + "M");   //最大可用内存
        map.put("usedMemorySize",new DecimalFormat("#.#").format(memoryUsage.getUsed() * 1.0 / 1024 / 1024) + "M");   //已使用的内存
        map.put("freePhysicalMemorySize",new DecimalFormat("#.##").format(osmxb.getFreePhysicalMemorySize() / 1024.0 / 1024 / 1024) + "G");   //剩余内存

        return success(map);
    }

}
